﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class PlayerController : MonoBehaviour
{
    public int attackChangeValue = 2000;
    public int maxHp = 100;
    public int hp = 100;
    public float speed = 15;
    public LayerMask backgroundLay;
    public float jump_power = 31;
    public int atk;
    public Text score;
    public Transform weapon;

    Animator am;
    int jumpCount = 1;
    bool isAttack;
    [HideInInspector]
    public bool isDmg;
    float dmgTime;
    float attackTime;
    bool isDie;
    int weaponAtk;

    void Start()
    {
        am = GetComponent<Animator>();
        weaponAtk = (int)Mathf.Floor(atk / 2);
    }

    void Update()
    {
        // 判断人物高度是否处于游戏最低点
        if (transform.position.y <= -45)
        {
            hp = 0;
            Destroy(gameObject, 1);
        }

        IsDie();
        
        // 被攻击状态转换
        dmgTime += Time.deltaTime;
        if (isDmg)
        {
            if (dmgTime >= 1.2F)
            {
                isDmg = false;
                dmgTime = 0;
            }
        }

        if (!isDie)
        {
            Move();
            Attack();
            CamareController();
        }
    }

    void IsDie()
    {
        if (hp <= 0)
        {
            isDie = true;
            am.SetTrigger("Die");
            Destroy(gameObject, 4);
        }
    }

    void Move()
    {
        //if (!isDie && hp > 0)
        //{
            if (Input.GetKey(KeyCode.A))
            {
                if (!isAttack && !isDmg)
                {
                    CameraMove.a = -5;
                    am.SetTrigger("Run");
                    //transform.localScale = new Vector3(Mathf.Abs(transform.localScale.x) * -1, transform.localScale.y, transform.localScale.z);
                    transform.rotation = Quaternion.Euler(0, 180, 0);
                    transform.Translate(transform.right * -speed * Time.deltaTime);
                }
            }
            if (Input.GetKeyUp(KeyCode.A))
            {
                am.ResetTrigger("Run");
                am.SetTrigger("Idle");
            }


            if (Input.GetKey(KeyCode.D))
            {
                if (!isAttack && !isDmg)
                {
                    CameraMove.a = 5;
                    am.SetTrigger("Run");
                    transform.rotation = Quaternion.Euler(0, 0, 0);
                    //transform.localScale = new Vector3(Mathf.Abs(transform.localScale.x), transform.localScale.y, transform.localScale.z);
                    transform.Translate(transform.right * speed * Time.deltaTime);
                }
            }
            if (Input.GetKeyUp(KeyCode.D))
            {
                am.ResetTrigger("Run");
                am.SetTrigger("Idle");
            }


            // 是否接触地面
            if (Physics2D.Raycast(transform.position, Vector2.down, 0.4F, backgroundLay))
            {
                jumpCount = 0;
            }

            if (Input.GetKeyDown(KeyCode.Space))
            {
                // 连跳检测
                if (jumpCount < 1)
                {
                    jumpCount++;
                    am.SetTrigger("Jump");
                    gameObject.GetComponent<Rigidbody2D>().velocity = transform.up * jump_power;
                }
            }
            if (Input.GetKeyUp(KeyCode.Space))
            {
                am.ResetTrigger("Jump");
                am.SetTrigger("Idle");
            }
        //}
    }


    void Attack()
    {
        if (Input.GetKeyDown(KeyCode.J))
        {
            isAttack = true;
            am.SetTrigger("Atk");

            if (int.Parse(score.text.Split(' ')[2]) >= attackChangeValue)
            {
                isAttack = false;
                Fire();
                atk = weaponAtk;
            }
        }

        if (isAttack)
        {
            attackTime += Time.deltaTime;
            if (attackTime >= 0.8F)
            {
                isAttack = false;
                attackTime = 0;
            }
        }

        if (Input.GetKeyUp(KeyCode.J))
        {
            am.ResetTrigger("Atk");
            am.SetTrigger("Idle");
        }

    }
    // 发射光波
    void Fire()
    {
        Transform darts = Instantiate(weapon, transform.FindChild("Fire").position, Quaternion.identity);
        darts.rotation = transform.rotation;
        darts.GetComponent<Rigidbody2D>().velocity = darts.right * speed * 1.7F;
        Destroy(darts.gameObject, 0.6F);
    }

    void CamareController()
    {
        if (jumpCount == 0)
        {
            if (Camera.main.orthographicSize > 7)
            {
                Camera.main.orthographicSize -= 10.0F * Time.deltaTime;
            }
            else
            {
                Camera.main.orthographicSize = 7;
            }

        }

        if (jumpCount == 1)
        {
            if (Camera.main.orthographicSize < 10)
            {
                Camera.main.orthographicSize += 10.0F * Time.deltaTime;
            }
        }
    }
}
